package com.small.sentinel.slots;

import com.small.common.core.spi.Spi;
import com.small.common.core.spi.SpiLoader;
import com.small.common.designPattern.behavioral.chainOfResponsibility.slotchain.AbstractLinkedProcessorSlot;
import com.small.common.designPattern.behavioral.chainOfResponsibility.slotchain.DefaultProcessorSlotChain;
import com.small.common.designPattern.behavioral.chainOfResponsibility.slotchain.ProcessorSlot;
import com.small.common.designPattern.behavioral.chainOfResponsibility.slotchain.ProcessorSlotChain;
import com.small.sentinel.slotchain.SlotChainBuilder;

import java.util.List;

@Spi(isDefault = true)
public class DefaultSlotChainBuilder implements SlotChainBuilder {

    @Override
    public ProcessorSlotChain build() {
        ProcessorSlotChain chain = new DefaultProcessorSlotChain();
        List<ProcessorSlot> sortedSlotList = SpiLoader.newInstance(ProcessorSlot.class).loadInstanceList();
        for (ProcessorSlot slot : sortedSlotList) {
            if (!(slot instanceof AbstractLinkedProcessorSlot)) {
                continue;
            }
            chain.addLast((AbstractLinkedProcessorSlot<?>) slot);
        }
        return chain;
    }
}
